<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

    <el-form-item label="描述" prop="description">
      <el-input v-model="dataForm.description" placeholder=""></el-input>
    </el-form-item>

    <el-form-item label="是否显示" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="logo" prop="logoUrl">
      <!-- <el-input v-model="dataForm.logoUrl" placeholder=""></el-input> -->
      <single-upload v-model="dataForm.logoUrl"></single-upload>
    </el-form-item>
    <el-form-item label="标签" prop="remark">
      <el-input v-model="dataForm.remark" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder=""></el-input>
    </el-form-item>

    <el-form-item label="地址" prop="url">
      <el-input v-model="dataForm.url" placeholder=""></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
  export default {
    components: { SingleUpload },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          category: '',
          charset: '',
          createTime: '',
          description: '',
          favoritesId: '',
          isDelete: '',
          lastModifyTime: '',
          logoUrl: '',
          remark: '',
          title: '',
          type: '',
          url: '',
          userId: ''
        },
        dataRule: {
          category: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          charset: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          favoritesId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lastModifyTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          logoUrl: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/admin/collect/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.category = data.collect.category
                this.dataForm.charset = data.collect.charset
                this.dataForm.createTime = data.collect.createTime
                this.dataForm.description = data.collect.description
                this.dataForm.favoritesId = data.collect.favoritesId
                this.dataForm.isDelete = data.collect.isDelete
                this.dataForm.lastModifyTime = data.collect.lastModifyTime
                this.dataForm.logoUrl = data.collect.logoUrl
                this.dataForm.remark = data.collect.remark
                this.dataForm.title = data.collect.title
                this.dataForm.type = data.collect.type
                this.dataForm.url = data.collect.url
                this.dataForm.userId = data.collect.userId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/collect/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'category': this.dataForm.category,
                'charset': this.dataForm.charset,
                'createTime': this.dataForm.createTime,
                'description': this.dataForm.description,
                'favoritesId': this.dataForm.favoritesId,
                'isDelete': this.dataForm.isDelete,
                'lastModifyTime': this.dataForm.lastModifyTime,
                'logoUrl': this.dataForm.logoUrl,
                'remark': this.dataForm.remark,
                'title': this.dataForm.title,
                'type': this.dataForm.type,
                'url': this.dataForm.url,
                'userId': this.dataForm.userId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
